<?php

namespace Modules\Admin\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use App\Http\Controllers\Controller as c;
class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
//        dd(app('hd-menu')->all());
        $userdate = User::paginate(5);

        return view('admin::admin.index',compact('userdate'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::admin.create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        User::create($request->all());
        session()->flash('success','用户添加成功');
        return redirect()->route('admin.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::admin.show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit($id)
    {
        //获取想要进行编辑的对应id的数据
        $user = User::find($id);
        return view('admin::admin.edit',compact('user'));
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request,$id)
    {
        //获取对应ID的用户数据
        $user = User::find($id);
        //获取用户进行编辑后的提交的post数据
        $post = $request->all();
//        dd($post);
        //把用户提交的数据对原来的数据进行替换
        $user->name = $post['name'];
        $user->email = $post['email'];
        if($request->input('password')){
            $user->password = bcrypt($post['password']);
        }
        $aa=$request->input('is_admin');
        if($aa){
            $user->is_admin=1;
        }else{
            $user->is_admin=0;
        }
        //保存
        $user->save();
        //设置闪存，并返回
        session()->flash('success','用户编辑成功');
        return redirect()->route('admin.index');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy(c $controller,$id)
    {
//        dd($id);
        $user = User::find($id);
        $controller->authorize('delete',$user);
        User::destroy($id);
        session()->flash('success','删除成功');
        return redirect()->route('admin.index');
    }
}
